package com.llamarun;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.util.Log;
import android.view.View;

public class Projectible extends NonPlayerObject{
	boolean ownedByLlama = false;
	boolean flying = false;
	public int speed =20; 
	
	public void improve()
	{
		
	}
	
	public void unimprove() {
		
	}
	
	public void shoot()
	{
		ownedByLlama = false;
		flying = true;
	}
	
	public void collideWithLlama(Llama llama)
	{
		//if projectibles are airborne we don't want llama to pick them up!
		if(!flying & !ownedByLlama)
		{
			this.ownedByLlama = true;
			llama.addProjectible(this);
		}
	}
	
	//override draw to only draw if the llama doesn't own this yet
	//...if the llama owns it then it will taking care of drawing this
	public void draw(Canvas canvas) {
		if(!ownedByLlama)
		{
			super.draw(canvas);
		}		
	}
	
	public void update(int gameTime)
	{
		if(flying==true)
		{
			this.fly();
		}
		else
		{
			super.update(gameTime);
		}
	}
	
	public void fly()
	{
		this.setX(this.getX()+speed);
		this.setY(this.getY()-speed);
	}

	//overwrite this because projectibles owned by the llama are allowed to 
	//exist off screen
	public boolean offScreen(View view)
	{
		if(ownedByLlama)
		{
			if (this.getX()>view.getWidth())
			{
				return true;
			}
				return false;
		}
		return super.offScreen(view);
	}
	
	public GameObject collideWithEnemy(ArrayList<GameObject> gameObjects) {
		//only do check if you are flying!
		if(this.flying){
			for (int i =0; i<gameObjects.size(); i++)
			{
				if(gameObjects.get(i).canIDie())
				{
					Log.e(null, "Checking if a projectible collides with an Enemy!");
					if(this.collision(gameObjects.get(i))){
						return gameObjects.get(i);
					}
				}
			}
		}
	return null;
	}
	
	static public int probabilityScore()
	{
		return 10;
	}
}
